﻿///<reference path="../jquery/jquery.js"/>
var CategoryPostList = function() {
	//private area
	var _ajaxDefaultOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8'
	};
	var _scrollTarget = null;
	var _selectcallback = function(msg, context) {
		msg = $.evalJSON(msg);
		if (msg.IsOk) {
			//Replace CategoryPostList
			$("#categoryPostsWrapper").html(msg.Context.CategoryPostListHtml);
			//Replace pager
			$("#pagerWrapper").html(msg.Context.PagerHtml);
			//Init pager again
			SimplePager.Init(msg.Context.Pager, CategoryPostList.Select1);
			//scroll to the scrollTarget
			if (_scrollTarget != null && _scrollTarget != "") {
				$.scrollTo(_scrollTarget);
			}

		} else {
			alert("Something goes wrong!");
		}
	}
	//public areas
	return {
		InitPager: "",
		Asmx: "",
		CategoryID: "",
		ViewPath: "~/Views/CategoryPostListView.ascx",
		Init: function(opts) {
			_scrollTarget = opts.scrollTarget;
			CategoryPostList.Asmx = opts.Asmx || "";
			CategoryPostList.CategoryID = opts.CategoryID || "";
			CategoryPostList.InitPager = opts.InitPagerStr || "";
			CategoryPostList.ViewPath = opts.ViewPath || CategoryPostList.ViewPath;
			//Do first load.
			//CategoryPostList.Select($.evalJSON(CategoryPostList.InitPager));//using webmethod
			CategoryPostList.Select1($.evalJSON(CategoryPostList.InitPager)); //using callback

		}, //endof Init
		DefaultDialogOpts: {
			modal: true,
			width: 600,
			height: 400,
			resizable: false,
			overlay: { opacity: 0.5, background: 'black' }
		},
		Select: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.CategoryID = CategoryPostList.CategoryID;
			data.ViewPath = CategoryPostList.ViewPath;
			var ajaxOpts = _ajaxDefaultOpts;
			ajaxOpts.url = CategoryPostList.Asmx + "SelectPosts";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace CategoryPostList
					$("#categoryPostsWrapper").html(msg.Context.CategoryPostListHtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.PagerHtml);
					//Init pager again
					SimplePager.Init(msg.Context.Pager, CategoryPostList.Select);
					//scroll to the scrollTarget
					if (_scrollTarget != null && _scrollTarget != "") {
						$.scrollTo(_scrollTarget);
					}
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		}, //endof Select
		Select1: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.CategoryID = CategoryPostList.CategoryID;
			data.ViewPath = CategoryPostList.ViewPath;
			WebForm_DoCallback('ctl00$cphBody$CategoryPostList1', $.toJSON(data), _selectcallback, 'post', null, false);
		} //endof Select1
	};
} ();